package yxy.game.pm2.bean.pubsub;

import java.util.Collection;
import java.util.HashSet;

import yxy.apple.util.PubSub.ISubscription;
import yxy.apple.util.PubSub.Publisher;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.CurrencyType;

/**
 * 物品背包发布
 */
public class CurrencyPublisher extends Publisher {
    static private CurrencyPublisher instance;

    public static CurrencyPublisher getInstance() {
        if (instance == null) {
            instance = new CurrencyPublisher();
        }
        return instance;
    }

    private CurrencyPublisher() {

    }

    public void publishAdd(String playerCode, Collection<Currency> currencies) {
        this.publish(new CurrencySubscription(playerCode, true, currencies));
    }

    public void publishMinus(String playerCode, Collection<Currency> currencies) {
        this.publish(new CurrencySubscription(playerCode, false, currencies));
    }

    public static class CurrencySubscription implements ISubscription {
        boolean add = true;
        private String playerCode;
        private Collection<CurrencyType> types;
        private Collection<Currency> currencies;

        public CurrencySubscription(String playerCode, boolean add, Collection<Currency> currencies) {
            this.add = add;
            this.playerCode = playerCode;
            HashSet<CurrencyType> types = new HashSet<>();
            for (Currency currency : currencies) {
                types.add(currency.getType());
            }
            this.types = types;
            this.currencies = currencies;
        }

        public boolean isAdd() {
            return add;
        }

        public String getPlayerCode() {
            return playerCode;
        }

        public Collection<CurrencyType> getTypes() {
            return types;
        }

        public Collection<Currency> getCurrencies() {
            return currencies;
        }

        public boolean checkTypes(Collection<CurrencyType> checker) {
            for (CurrencyType type : checker) {
                if (types.contains(type)) {
                    return true;
                }
            }
            return false;
        }
    }
}
